package com.neusoft.neuiotms.dcom.converter.insurance;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.neusoft.bizcore.web.dto.AbstractConverter;
import com.neusoft.bizcore.web.utils.id.BaseIdGenerator;
import com.neusoft.neuiotms.dcom.dto.insurance.InsuranceReportTaskDTO;
import com.neusoft.neuiotms.dcom.model.enums.InsuranceReportTaskStatus;
import com.neusoft.neuiotms.dcom.model.insurance.InsuranceReportTask;
import com.neusoft.neuiotms.dcom.repository.insurance.InsuranceReportTaskRepository;

@Component
public class InsuranceReportTaskConverter
        extends AbstractConverter<InsuranceReportTask, InsuranceReportTaskDTO> {

    @Autowired
    private BaseIdGenerator insuranceReportTaskCodeGenerator;
    @Autowired
    private InsuranceReportTaskRepository insuranceReportTaskRepository;

    @Override
    public InsuranceReportTask toModel(final InsuranceReportTaskDTO dto) {

        InsuranceReportTask model;
        if (dto.isNew()) {
            model = new InsuranceReportTask();
            if (StringUtils.isEmpty(dto.getCode())) {
                model.setCode(this.insuranceReportTaskCodeGenerator.generateCode());
                model.setUnitCode(dto.getUnitCode());
                model.setStartTime(dto.getStartTime());
                model.setEndTime(dto.getEndTime());
                model.setCronExp(dto.getCronExp());
                model.setSorting(dto.getSorting());
            } else {
                model.setCode(dto.getCode());
            }
        } else {
            model = this.insuranceReportTaskRepository.getOne(dto.getId());
        }

        model.setReportPersonCode(dto.getReportPersonCode());
        model.setReportPersonName(dto.getReportPersonName());
        model.setNormal(dto.getNormal());
        model.setAbnormalSituation(dto.getAbnormalSituation());
        model.setStatus(InsuranceReportTaskStatus.valueOf(dto.getStatus()));

        return model;
    }

    @Override
    public InsuranceReportTaskDTO toDTO(final InsuranceReportTask model, final boolean forListView) {

        final InsuranceReportTaskDTO dto = new InsuranceReportTaskDTO();

        dto.setId(model.getId());
        dto.setCode(model.getCode());
        dto.setReportPersonCode(model.getReportPersonCode());
        dto.setReportPersonName(model.getReportPersonName());
        dto.setUnitCode(model.getUnitCode());
        dto.setStartTime(model.getStartTime());
        dto.setEndTime(model.getEndTime());
        dto.setCronExp(model.getCronExp());
        dto.setSorting(model.getSorting());
        dto.setNormal(model.getNormal());
        dto.setAbnormalSituation(model.getAbnormalSituation());
        dto.setStatus(model.getStatus().name());

        return dto;
    }

}
